package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;

import classes.Funcionario;
import fachada.Fachada;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.Button;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class TelaBuscaFuncionario extends JFrame {
	private JPanel contentPane;
	private JTable table;
	List<Funcionario> lista = new ArrayList<Funcionario>();
	private JTextField txtMatricula;
	private JTable tableBusca;
	Funcionario fun = new Funcionario();
	long matricula;
	long i;	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaBuscaFuncionario frame = new TelaBuscaFuncionario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	 public void listarFuncionarios(){
		try {
 			lista = Fachada.getInstance().consultarTodosFuncionario();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "deu merda!!!");
			//Logger.getLogger(TelaBuscaFuncionario.class.getName()).log(Level.SEVERE, null, e);
		}
 		DefaultTableModel modelo = new DefaultTableModel();
	    modelo.setColumnIdentifiers(new String[]{"Codigo" ,"Nome", "CPF"});
	    for(Funcionario f : lista) { 
                 modelo.addRow(new String[]{f.getId() + "", f.getNome()+"",f.getCpf() + ""});
	    }
	    	table.setModel(modelo);
	 }
	
	 
	 public void buscaPorId(long matricula){
	 try {
		  fun =  Fachada.getInstance().consultarFuncionarioPorId(matricula);
	 	} catch (Exception e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "DEU MERDA!");
		}
		
			JOptionPane.showMessageDialog(null, fun.getNome());
	 		DefaultTableModel modelo = new DefaultTableModel();
		    modelo.setColumnIdentifiers(new String[]{"Codigo" ,"Nome", "CPF"});
		    modelo.addRow(new String[]{fun.getId() + "", fun.getNome()+"",fun.getCpf() + ""});
	    	tableBusca.setModel(modelo);
	 }
	 
	 
	 public void resertar(){
		DefaultTableModel modelo = new DefaultTableModel();
		modelo.setColumnIdentifiers(new String[]{"Codigo" ,"Nome", "CPF"});
		modelo.addRow(new String[]{"", "",""});
		tableBusca.setModel(modelo);
		 
	 }
	 
	 
	 
	/**
	 * Create the frame.
	 */
	public TelaBuscaFuncionario() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		table = new JTable();
		table.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, true));
		table.setBounds(173, 55, 1008, 130);
		contentPane.add(table);
		
		JLabel lblListandoFuncionrios = new JLabel("Listando Funcion\u00E1rios");
		lblListandoFuncionrios.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblListandoFuncionrios.setBounds(173, 23, 179, 21);
		contentPane.add(lblListandoFuncionrios);
		
		JLabel lblBucandoPorMatricula = new JLabel("Bucando por Matricula");
		lblBucandoPorMatricula.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblBucandoPorMatricula.setBounds(173, 231, 179, 21);
		contentPane.add(lblBucandoPorMatricula);
		
		JLabel lblNewLabel = new JLabel("Matr\u00EDcula :");
		lblNewLabel.setBounds(173, 263, 66, 14);
		contentPane.add(lblNewLabel);
		
		txtMatricula = new JTextField();
		txtMatricula.setBounds(239, 260, 264, 20);
		contentPane.add(txtMatricula);
		txtMatricula.setColumns(10);
		
		tableBusca = new JTable();
		tableBusca.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
		        i = Integer.parseInt(tableBusca.getValueAt(tableBusca.getSelectedRow(), 0).toString());
		       
				
			}
		});
		tableBusca.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, true));
		tableBusca.setBounds(173, 303, 1008, 72);
		contentPane.add(tableBusca);
		
		Button button = new Button("Buscar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 matricula = Long.parseLong(txtMatricula.getText());
				 buscaPorId(matricula);
			}
		});
		button.setBounds(522, 260, 70, 22);
		contentPane.add(button);
		
		Button button_1 = new Button("Atualizar");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// cerregar todos os dados nesse frame mesmo
				
				
				
				
			}
		});
		button_1.setBounds(1111, 665, 70, 23);
		contentPane.add(button_1);
		
		Button button_2 = new Button("Fechar");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TelaPricipal tp = new TelaPricipal();
				tp.setVisible(true);
				dispose();
			}
		});
		button_2.setBounds(900, 665, 70, 23);
		contentPane.add(button_2);
		
		JButton btnRemover = new JButton("Remover");
		btnRemover.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				// removendo funcionario  
				  try {
					Fachada.getInstance().removerFuncionario(i);
				} catch (Exception e) {
			    	e.printStackTrace();
				    JOptionPane.showMessageDialog(null, "deu merda!!!");
				}
				
				 // Resetando o table de busca 
				 resertar();
				 
				 // Da um refresh da linsta de todos os funcionario 
				 listarFuncionarios();
			}
		});
		btnRemover.setBounds(999, 665, 91, 23);
		contentPane.add(btnRemover);
		setSize(1280,750);
        this.setLocationRelativeTo(null);
        listarFuncionarios();
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
